/**
 * @author JackGao
 *			email:jackgao.china@gmail.com
 *  date：2016-6-23
 * 
 */
package cn.cuit.ds.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import javassist.expr.NewArray;
import cn.cuit.ds.dao.IAddressDao;
import cn.cuit.ds.dao.IGoodsDao;
import cn.cuit.ds.dao.IOrderDetailDao;
import cn.cuit.ds.dao.IOrdersDao;
import cn.cuit.ds.dao.IUserinfoDao;
import cn.cuit.ds.dao.impl.AddressDao;
import cn.cuit.ds.dao.impl.GoodsDao;
import cn.cuit.ds.dao.impl.OrderDetailDao;
import cn.cuit.ds.dao.impl.OrdersDao;
import cn.cuit.ds.dao.impl.UserinfoDao;
import cn.cuit.ds.domain.Address;
import cn.cuit.ds.domain.Goods;
import cn.cuit.ds.domain.Goods2Orders;
import cn.cuit.ds.domain.OrderDeatil;
import cn.cuit.ds.domain.Orders;
import cn.cuit.ds.domain.User;
import cn.cuit.ds.service.IUserService;
import cn.cuit.ds.utils.OtherUtils;
import cn.cuit.ds.utils.PageBean;

@Service(value="UserService")
public class UserService implements IUserService {

	@Resource(name="UserinfoDao")
	private IUserinfoDao userinfoDao ;
	public void setUserinfoDao(IUserinfoDao userinfoDao) {
		this.userinfoDao = userinfoDao;
	}
	
	
	
	@Resource(name="AddressDao")
	private	IAddressDao addressDao ;
	public void setAddressDao(IAddressDao addressDao) {
		this.addressDao = addressDao;
	}
	
	@Resource(name="OrderDetailDao")
	private IOrderDetailDao orderDetailDao ;
	public void setOrderDetailDao(IOrderDetailDao orderDetailDao) {
		this.orderDetailDao = orderDetailDao;
	}
	
	@Resource(name="GoodsDao")
	private GoodsDao goodsDao ;
	public void setGoodsDao(GoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}
	
	@Resource(name="OrdersDao")
	private IOrdersDao ordersDao ; 
	public void setOrdersDao(IOrdersDao ordersDao) {
		this.ordersDao = ordersDao;
	}
	
	
	
	
	
	public int regist(User user) {
		if (userinfoDao.findByName(user.getUserName()) != null) {
			//	这个用户名已经存在
			return 0;
		}
		//	用户名不存在，可以注册
		user.setUserId(OtherUtils.getUUID());
		user.setRegisTime(OtherUtils.getDate());
		userinfoDao.add(user);
		return 1;
	}

	public User login(User user) {
		return userinfoDao.findByNameAndPwd(user) ;
	
	}

	/**
	 * 表单hidden存起来提交
	 */
	public void addAddress(Address address) {
		address.setAddressId(OtherUtils.getUUID());
		addressDao.add(address);
	}


	public int checkout(Orders orders) {
		orders.setOrdersId(OtherUtils.getUUID());
		orders.setOrderTime(OtherUtils.getDate());
		ordersDao.add(orders);
		return 0;
	}

	public List<Address> getAllAddress(String userId) {
		return addressDao.getAllByUserId(userId);
	}

	public void deleteAddress(String addressId) {
		addressDao.deleteByAddressId(addressId);
		
	}

	
	public void updateAddress(Address address) {
		addressDao.update(address);
		
	}

	public Address getAddressById(String addressId) {
		return addressDao.getById(addressId);
	}

	public List<OrderDeatil> getAllOrderDetailById(String userId) {
		return orderDetailDao.getAllDetailByuserId(userId);
	}
	
	
	
	

	public List<Goods> getAllGoods() {
		
		return goodsDao.getAll();
	}
	
	public void getAllGoods(PageBean<Goods> pb){
		 goodsDao.getAll(pb);
	}

	/**
	 * 舍弃该用法
	 */
	public void getAll(PageBean<Goods> pageBean) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 根据一群id查询
	 */
	public List<Goods> getAllGoodsByIds(List<String> ids) {
		if (ids==null) {
			return null;
		}
		ArrayList<Goods> list = new ArrayList<Goods>();
		for (String id : ids) {
			Goods goods = goodsDao.findById(id);
			list.add(goods);
		}
		return list;
	}





	public void updateDatabase(Goods goods) {
		// TODO Auto-generated method stub
		goodsDao.subTotalNum(goods.getGoodsId());
		goodsDao.addGoodsNum(goods.getGoodsId());
	}





	public List<OrderDeatil> getAllOrderDetailByIdAndTime(String userId,
			String timefrom, String timeto) {
		return orderDetailDao.getDetailByUserIdAndTime(userId, timefrom, timeto);
		
	}





	public List<User> getAllUsres() {
		// TODO Auto-generated method stub
		return userinfoDao.getAll();
	}





	public void deleteUser(String userId) {
		// TODO Auto-generated method stub
		userinfoDao.deleteOneUser(userId);
	}





	public User getUserById(String userId) {
		// TODO Auto-generated method stub
		return userinfoDao.findById(userId);
	}





	public void update(User user) {
		// TODO Auto-generated method stub
		userinfoDao.update(user);
	}


	
	
}
